const config = require('../config/config')
const rp = require('request-promise')
const { Picture } = require('../models/')
const BaseService = require('./base_service')
class PictureService extends BaseService {

  // 根据id获取图片
  static async getById (params = {}) {
    const res = await Picture.getById(params)
    let data = res && res.length && res[0]
    return data
  }

  // 根据图片id获取标签列表
  static async getPicActiveTags (params = {}) {
    const res = await Picture.getPicActiveTags(params)
    let data = []
    res.forEach(ele => {
      data.push({ id: ele['tag_id'], name: ele['name'] })
    })
    return data
  }

  // 根据图片获取其tag_id列表
  static async getTabIdsByPicId (params = {}) {
    const relations = await Picture.getTabIdsByPicId(params)
    let ids = []
    relations.forEach(relation => {
      ids.push(relation['tag_id'])
    })
    return ids
  }

  // 获取列表
  static async getList (params = {}) {
    const res = await Picture.getList(params)
    return res
  }

   // 获取列表
   static async getListTotal (params = {}) {
    const res = await Picture.getListTotal(params)
    return res
  }

  // 获取列表
  static async getListByTagId (params = {}) {
    const res = await Picture.getListByTagId(params)
    return res
  }
  
  // 获取列表
  static async getListByTagIdTotal (params = {}) {
    const res = await Picture.getListByTagIdTotal(params)
    return res
  }

  // 获取列表
  static async getListInPicTag (params = {}) {
    const res = await Picture.getListInPicTag(params)
    return res
  }

  // 获取列表
  static async getListInPicTagTotal (params = {}) {
    const res = await Picture.getListInPicTagTotal(params)
    return res
  }

  // 获取列表
  static async getRelateByPicId (params = {}) {
    const res = await Picture.getRelateByPicId(params)
    return res
  }

  // 获取列表总数
  static async getRelateByPicIdTotal(params = {}){
    const res = await Picture.getRelateByPicIdTotal(params)
    return res
  }

  // 获取最新标签
  static async getLatestTagList(params = {}){
    const tagIdRows = await Picture.getLatestTagList(params)
    return tagIdRows
  }


  // 获取列表
  static async download (params = {}) {
    function getClientIP(req) {
      return req.headers['x-forwarded-for'] || // 判断是否有反向代理 IP
      req.connection.remoteAddress || // 判断 connection 的远程 IP
      req.socket.remoteAddress || // 判断后端的 socket 的 IP
      req.connection.socket.remoteAddress
    }
    // let names = params.name.split('.')
    // let res = await rp({
    //     method: 'POST',
    //     uri: config.ossApi.uri,
    //     headers: {
    //       'x-version': config.ossApi.apiVersion,
    //       'x-app-id': config.ossApi.apiAppId
    //     },
    //     body: {
    //       suffix: names[1],
    //       path: 'thumb/',
    //       name: names[0]
    //     },
    //     json: true
    // })
    let client_ip = getClientIP(params.req)
    client_ip= client_ip.replace(/f/g, '').replace(/:/g, '')
    await Picture.updatePictureDownLoad({ file_name: params.name })
    let [pic] = await Picture.getPictureByFileName({ file_name: params.name })
    if (pic['id'] && params.name) {
      await Picture.insertPictureDownLoadLog({ file_name: params.name, pid: pic.id, client_ip: client_ip || '0.0.0.0' })
    }
    return {}
  }

}

module.exports = PictureService
